<template>
  <div>
    <a-card class="card-header">
      <a-form-model :model="queryParam">
        <a-row :gutter="24">
          <a-col :span="5">
            <a-form-model-item
              label="加工单号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.processNum"
                placeholder="请输入加工单号"
              ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="5">
            <a-form-model-item
              label="计划单号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.planNum"
                placeholder="请输入计划单号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="5">
            <a-form-model-item
              label="单据日期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-range-picker
                v-model.trim="queryParam.billTime"
                :placeholder="['开始时间', '结束时间']"
                @change="onChange"
                format="YYYY-MM-DD "
              />
            </a-form-model-item>
          </a-col>

          <a-col :span="5">
            <a-form-model-item
              label="状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select
                allowClear
                style="width:100%"
                v-model.trim="queryParam.status"
                placeholder="请选择状态"
              >
                <a-select-option
                  v-for="item in processingOption"
                  :key="item.id"
                  :value="item.id"
                >
                  {{ item.dictName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="2">
            <a-button
              type="primary"
              style="margin-top:3px"
              @click="handleSearch"
              >查询</a-button
            >
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>

    <!-- 表格区域 -->
    <a-card class="card-content">
      <div class="contetnBox">
        <a-row :gutter="24" class="headerBox">
          <a-col :span="22">
            <a-button type="primary" @click="handleToDetail('/order/add')"
              >新增加工单</a-button
            >
          </a-col>
          <a-col :span="2">
            <a-button type="primary" @click="handleAllList"
              >全部加工单</a-button
            >
          </a-col>
        </a-row>
        <br />
        <a-table
          :columns="columns"
          :data-source="tableData"
          :loading="loading"
          :pagination="pagination"
          @change="handleTableChange"
          :rowKey="(record, index) => record.id"
          bordered
        >
          <template slot="action" slot-scope="text, record">
            <a @click="handleToDetail('/order/edit', record)">编辑</a>
            <a-divider type="vertical" />
            <a @click="handleOrderResume(record)">工单履历</a>
            <a-divider type="vertical" v-hasPromise="'orderDel'" />
            <a-popconfirm
              v-hasPromise="'orderDel'"
              okText="确定"
              cancelText="取消"
              title="确定删除吗?"
              @confirm="() => handleDelete(record)"
            >
              <a>删除</a>
            </a-popconfirm>
            <a-divider type="vertical" />
            <a-icon
              type="qrcode"
              @click.stop="handleCode(record)"
              style="fontSize: 16px;color: #40a9ff;"
            />
          </template>
        </a-table>
      </div>
    </a-card>
    <OrderResume ref="OrderResume" />

    <!-- 二维码 -->
    <qrCode
      ref="qrCode"
      :label="qrModel.label"
      :text="qrModel.text"
      :qrSize="320"
    ></qrCode>
  </div>
</template>

<script>
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import qrCode from "@/components/qrCode";
import { columns } from "./components/tableConfig";
import api from "@/api/apiServer";
import OrderResume from "./components/OrderResume.vue";
import { CodeNo } from "@/unit/QRCodeConfig";
export default {
  mixins: [tableMinxins],
  components: { OrderResume, qrCode },
  data() {
    return {
      columns,
      processingOption: [],
      pickingOption: [],
      qrModel: {
        label: "", // 二维码底部内容
        text: "", // 二维码内容
      },
    };
  },
  activated() {
    this.getList();
  },
  mounted() {
    this.getList();
  },
  methods: {
    handleCode(record) {
      let { processNum, id, goodsId } = record;
      let newObj = { id, processNum, goodsId, QRCode: CodeNo.ProcessingOrder };
      let text = JSON.stringify(newObj);
      this.$refs.qrCode.title = "加工单二维码";
      this.qrModel.label = `${processNum} `;
      this.qrModel.text = text;
      this.$refs.qrCode.list = this.qrModel.label.split(",");
      this.$refs.qrCode.showModal();
    },
    // 获取数据来源
    async getDictFindByCode(parentCode, key) {
      const res = await api.DictFindByCode({ parentCode });
      this[key + "Option"] = res.data;
    },
    onChange(date, dateString) {
      this.$set(this.queryParam, "startTime", dateString[0]);
      this.$set(this.queryParam, "endTime", dateString[1]);
    },
    // 格式化状态
    caseStatus(value, option) {
      let dictName = "";
      option.forEach((item) => {
        if (item.id === value) {
          dictName = item.dictName;
        }
      });
      return dictName;
    },
    // 获取列表数据
    async getList(arg) {
      this.loading = true;
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.pagination.current = 1;
      }
      var params = this.getQueryParams();
      const res = await api.WorksheetList(params);
      this.tableData = res.data.data;
      this.pagination.total = res.data.total;
      this.loading = false;
      this.getDictFindByCode("dict.processing", "processing");
      this.getDictFindByCode("dict.picking", "picking");
    },
    // 删除
    async handleDelete(record) {
      let params = {
        id: record.id,
      };
      const res = await api.WorksheetDelete(params);
      this.$message.success(res.message);
      this.getList();
    },
    // 工单履历
    handleOrderResume(record) {
      this.$refs.OrderResume.showDrawer(record);
    },
    handleToDetail(path, record) {
      let query = {};
      if (record) {
        query.id = record.id;
      }
      this.$router.push({ path, query });
    },
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
  button {
    margin-right: 13px;
  }
}
.card-content {
  .contetnBox {
    min-height: 809px;
    border: 1px solid #ebedf0;
    border-radius: 2px;
    padding: 20px 0;
    .headerBox {
      padding: 0 20px;
    }
  }
}
::v-deep .ant-form-item {
  margin-bottom: 0;
}
</style>
